package com.nicky.kids;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

import com.nicky.kids.Utility.DateUtil;
import com.nicky.kids.Utility.IntentUtil;
import com.nicky.kids.Utility.SDCardUtil;
import com.nicky.kids.Utility.TLog;

import java.io.File;

public class HomeActivity extends Activity {
    private static final String TAG = "nick_HomeActivity";
    private final int CAMERA_RESULT = 0;

    private Button mCaptureBtn;
    private StringBuffer mPhotoName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);

        //mPhotoName = new StringBuffer();

        mCaptureBtn = (Button)this.findViewById(R.id.capture_btn);
        mCaptureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    mPhotoName = new StringBuffer();
                    mPhotoName.append("IMG_");
                    mPhotoName.append(DateUtil.dateToString());
                    mPhotoName.append(".jpg");

                    TLog.e(TAG, mPhotoName.toString());

                    File mPhotoFile = new File(SDCardUtil.getKidsDir(), mPhotoName.toString());
                    if (!mPhotoFile.exists()) {
                        mPhotoFile.createNewFile();
                    }

                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile));
                    startActivityForResult(intent, CAMERA_RESULT);




                    //choose pic
//                    Intent intent = new Intent();
//                    intent.setType("image/*");
//                    intent.setAction(Intent.ACTION_GET_CONTENT);
//                    startActivityForResult(intent, 2);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
	}


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_RESULT) {
            Intent intent = new Intent(HomeActivity.this, ModifyActivity.class);
            intent.putExtra("name", mPhotoName.toString());
            IntentUtil.startIntent(HomeActivity.this, intent);
        }

    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.home, menu);
		return true;
	}

}
